package com.bsoft.gol.hbs.service;


import com.bsoft.gol.hbs.bean.dto.HealthInfoInteractionCountUpdateDTO;
import com.bsoft.gol.hbs.bean.exception.ServiceException;
import com.bsoft.gol.hbs.dao.HealthInfoDAO;
import com.bsoft.gol.hbs.utils.CommonUtils;
import ctd.util.JSONUtils;
import hcn.base.healthinfo.qo.ArticleInteractionRequest;
import hcn.bean.PushMessageBean;
import hcn.common.MqConstant;
import hcn.service.base.sendMsg.IMessageSyncService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.boot.rocketmq.annotation.RocketMQSubscription;
import org.boot.rocketmq.consumer.AbstractRocketMQConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pcn.enums.CmsActionEnum;

@Slf4j
@Service
@RocketMQMessageListener(topic = MqConstant.TOPIC_BIZ_SEND_INTERACTION, selectorExpression = MqConstant.TOPIC_BIZ_SEND_INTERACTION, consumerGroup = "information")
public class SendInteractionMsgListener extends AbstractRocketMQConsumer implements RocketMQListener<MessageExt> {


    @Autowired
    private HealthInfoDAO healthInfoDAO;

    /**
     * 发送各种消息
     */
    @RocketMQSubscription(tag = MqConstant.TOPIC_BIZ_SEND_INTERACTION, retryTime = 3)
    public void onSmsSendRequest(ArticleInteractionRequest articleInteractionRequest) {
        log.info("SEND_INTERACTION={}", JSONUtils.toString(articleInteractionRequest));
        String articleId = articleInteractionRequest.getArticleId();
        HealthInfoInteractionCountUpdateDTO dto = new HealthInfoInteractionCountUpdateDTO();
        dto.setArticleId(articleId);
        CmsActionEnum byName = CmsActionEnum.getByName(articleInteractionRequest.getAction());
        if (CommonUtils.isEmpty(byName)) {
            throw new ServiceException("不支持的操作类型");
        }
        int add = 1;
        if (articleInteractionRequest.isCancel()) {
            add = -1;
        }
        switch (byName) {
            case likes:
                dto.setLikesCountAdd(add);
                break;
            case mark:
                dto.setCollectionCountAdd(add);
                break;
            case share:
                dto.setShareCountAdd(add);
                break;
        }
        healthInfoDAO.updateInteractionCount(dto);
    }

}